package cn.interestingshop.service.baseOrder;

import cn.interestingshop.dao.BaseOrderDao;
import cn.interestingshop.dao.BaseOrderDaoImpl;
import cn.interestingshop.dao.UserAddressDao;
import cn.interestingshop.dao.UserAddressDaoImpl;
import cn.interestingshop.entity.BaseOrder;
import cn.interestingshop.utils.DataSourceUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BaseOrderServiceImpl implements BaseOrderService{
    @Override
    public int insert(BaseOrder baseOrder) {

        Connection conn=null;
        int rs=0;

        try {
            conn= DataSourceUtil.openConnection();
            BaseOrderDao baseOrderDao=new BaseOrderDaoImpl(conn);

            rs=baseOrderDao.insert(baseOrder);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }

        return rs;
    }

    @Override
    public BaseOrder selectOne(int id) {

        Connection conn=null;
        BaseOrder baseOrder=new BaseOrder();

        try {
            conn= DataSourceUtil.openConnection();
            BaseOrderDao baseOrderDao=new BaseOrderDaoImpl(conn);

            baseOrder=baseOrderDao.selectOne(id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }

        return baseOrder;
    }

    @Override
    public List<BaseOrder> selectOneAll(int id,Integer currentPageNo, Integer pageSize) {
        Connection conn=null;
        List<BaseOrder> list=new ArrayList<>();

        try {
            conn= DataSourceUtil.openConnection();
            BaseOrderDao baseOrderDao=new BaseOrderDaoImpl(conn);

            list=baseOrderDao.selectOneAll(id,currentPageNo,pageSize);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }

        return list;
    }

    @Override
    public List<BaseOrder> selectAll(Integer currentPageNo, Integer pageSize) {
        Connection conn=null;
        List<BaseOrder> list=new ArrayList<>();

        try {
            conn= DataSourceUtil.openConnection();
            BaseOrderDao baseOrderDao=new BaseOrderDaoImpl(conn);

            list=baseOrderDao.selectAll(currentPageNo,pageSize);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }

        return list;
    }

    @Override
    public int selectOneAllCount(int id) {
        Connection conn=null;
        int rs=0;

        try {
            conn= DataSourceUtil.openConnection();
            BaseOrderDao baseOrderDao=new BaseOrderDaoImpl(conn);

            rs=baseOrderDao.selectOneAllCount(id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }

        return rs;
    }

    @Override
    public int selectAllCount() {
        Connection conn=null;
        int rs=0;

        try {
            conn= DataSourceUtil.openConnection();
            BaseOrderDao baseOrderDao=new BaseOrderDaoImpl(conn);

            rs=baseOrderDao.selectAllCount();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }

        return rs;
    }

    @Override
    public int xgZf(String orderNo) {
        Connection conn=null;
        int rs=0;

        try {
            conn= DataSourceUtil.openConnection();
            BaseOrderDao baseOrderDao=new BaseOrderDaoImpl(conn);

            rs=baseOrderDao.xgZf(orderNo);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }

        return rs;
    }
}
